package com.aidong.videoComment.action;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.videoComment.biz.TrnVideoPraiseBiz;
import com.aidong.videoComment.entity.TrnVideoPraise;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("json")
@Namespace("/")
public class TrnVideoPraiseAction implements ModelDriven<TrnVideoPraise> {
	private TrnVideoPraise trnVideoPraise;
	
	@Override
	public TrnVideoPraise getModel() {
		if (trnVideoPraise == null) {
			trnVideoPraise = new TrnVideoPraise();
		}
		return trnVideoPraise;
	}
	private Map<String, Object> map;

	@JSON
	public Map<String, Object> getMap() {
		return map;
	}
	private TrnVideoPraiseBiz trnVideoPraiseBiz; 
	@Autowired
	public void setTrnVideoPraiseBiz(TrnVideoPraiseBiz trnVideoPraiseBiz) {
		this.trnVideoPraiseBiz = trnVideoPraiseBiz;
	} 
	
	@Action(value = "addVideoPraise", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String addVideoPraise() {
		map = new HashMap<String, Object>();
		
		if(trnVideoPraise.getVideoId() >0 && trnVideoPraise.getIdongId() > 0){
			try{
				/*Map<String,Object> mps=new HashMap<String,Object>();
				mps.put("idongId", trnVideoPraise.getIdongId());
				mps.put("videoId", trnVideoPraise.getVideoId());
				TrnVideoPraise praise=trnVideoPraiseBiz.getVideoPraise(mps);
				if(praise!=null){
					if(praise.getPraiseStatus()==0){
						trnVideoPraise.setPraiseStatus(1);
						map.put("result", "已取消点赞");
					}else{
						trnVideoPraise.setPraiseStatus(0);
						map.put("result", "已点赞");
					}
					trnVideoPraiseBiz.editVideoPraise(trnVideoPraise);
				}else{*/
					trnVideoPraise.setPraiseStatus(0);
					trnVideoPraiseBiz.addVideoPraise(trnVideoPraise);
					map.put("result", "已点赞");
				//}
				map.put("code", 1);
				
				return "succ";
			} catch(Exception e) {
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
		}else{
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
	}
	
}
